using System.Collections.Generic;
using System.Windows.Forms;
using Mbs.Elements;
using Mbs.Globalization;
using Mbs.Modularity.AddIns;
using Mbs.ComponentModel.Container;

namespace Mbs.Windows.Elements.Builders
{
    [Component(Id = "ToolStrip.Button", Service = typeof(IElementBuilder))]
    public class ToolStripButtonBuilder : ToolStripItemBuilder<ToolStripButton>
    {
        protected override ToolStripButton CreateItem(IAddInItem addInItem)
        {
            return new MbsToolStripButton(addInItem);
        }

        private class MbsToolStripButton : ToolStripButton,Mbs.Globalization.ISupportGlobalization,IAddInItemHost
        {
            public IAddInItem AddInItem { get; private set; }
            public MbsToolStripButton(IAddInItem addInItem)
            {
                AddInItem = addInItem;
                IsSupportGlobalization = true;

                this.Initialize(addInItem);
                InitializeResource();
            }


            public bool IsSupportGlobalization { get; set; }

            public void InitializeResource()
            {
                Text = StringParser.Parse(AddInItem.Label);
                Image = AddInItem.Properties.Contains("Icon") ? ResourceRepository.Images.Get(AddInItem.Properties.Get<string>("Icon")) : null;
                if (AddInItem.Properties.Contains("ToolTip"))
                    ToolTipText = StringParser.Parse(AddInItem.Properties.Get<string>("ToolTip"));
            }

            public void RefreshResource()
            {
                InitializeResource();
            }
        }
       
        protected override void OnInitInstance(object caller, IAddInItem element, List<object> subItems, ToolStripItem instance)
        {
            //TODO:test
            //if (element.IsSite())
                ElementService.Sites.RegisterSite(element.Path, instance);

            BindCommand(element, instance, "Click");
        }
    }
}
